package com.uchengwang.ui.cm_ui.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.GridViewWithHeaderAndFooter;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.login.adapter.ChooseCycleAdapter;

import java.util.List;
/**
 * Created by U城网 on 2018/2/7.
 */

public class ChooseCycleActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private Button choose_open;
    private ChooseCycleAdapter adapter;
    private int page = 1, totalPage;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private GridViewWithHeaderAndFooter choose_gv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_grid_load);
        setTitle("大学社圈");
        setRightText("跳过", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openUcity();
            }
        });
        initView();
    }

    private void initView(){
        choose_gv = (GridViewWithHeaderAndFooter) findViewById(R.id.load_gv);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.load_refresh);
        choose_open = (Button) findViewById(R.id.choose_open);
        choose_open.setOnClickListener(this);

        initData();
    }

    private void initData(){
        adapter = new ChooseCycleAdapter(this);
        choose_gv.setAdapter(adapter);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.setLoadMoreEnable(true);
                mSwipeRefreshLayout.loadMoreComplete(true);
                getData(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 获取社圈
     * */
    private void getData(final boolean isRefresh){
        CycleHttpUtils.getUtils().getCycles(this, 0, 0, page, 10, new OnHttpResultListener<CycleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                totalPage = total;

                if (list != null){
                    if (isRefresh){
                        adapter.setList(list);
                    }else {
                        adapter.getList().addAll(list);
                        adapter.notifyDataSetChanged();
                    }

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.choose_open:
                openUcity();
                break;
        }
    }

    private void openUcity(){
        startActivity(new Intent(this, MainActivity.class));
    }
}
